<?php

module_load_include('inc', 'taxonomy', 'taxonomy.admin');

/*
 *  vocabulary
 */

function system_reform_taxonomy_overview() {
  $output = drupal_get_form('system_reform_taxonomy_vocabularies_form');
  return $output;
}

function system_reform_taxonomy_vocabularies_form() {
  $form = taxonomy_overview_vocabularies();
  foreach ($form as $k => $v) {
    if (is_numeric($k)) {
      $form[$k]['edit']['#value'] = '<a href="' . url('admin/systemreform/taxonomy/edit/vocabulary/' . $v['#vocabulary']['vid']) . '">' . t('edit vocabulary') . '</a>';
      $form[$k]['list']['#value'] = '<a href="' . url('admin/systemreform/taxonomy/' . $v['#vocabulary']['vid']) . '">' . t('list terms') . '</a>';
      $form[$k]['add']['#value'] = '<a href="' . url('admin/systemreform/taxonomy/' . $v['#vocabulary']['vid'] . '/add/term') . '">' . t('add terms') . '</a>';
    }
  }
  $form['#theme'] = 'taxonomy_overview_vocabularies';
  $form['#submit'][] = 'taxonomy_overview_vocabularies_submit';
  return $form;
}

function system_reform_taxonomy_vocabulary_confirm_delete(&$form_state, $vid) {
  $vocabulary = taxonomy_vocabulary_load($vid);

  $form['type'] = array('#type' => 'value', '#value' => 'vocabulary');
  $form['vid'] = array('#type' => 'value', '#value' => $vid);
  $form['name'] = array('#type' => 'value', '#value' => $vocabulary->name);
  return confirm_form($form, t('Are you sure you want to delete the vocabulary %title?', array('%title' => $vocabulary->name)), 'admin/systemreform/taxonomy', t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'), t('Delete'), t('Cancel'));
}

function system_reform_taxonomy_vocabulary_confirm_delete_submit($form, &$form_state) {
  $status = taxonomy_del_vocabulary($form_state['values']['vid']);
  drupal_set_message(t('Deleted vocabulary %name.', array('%name' => $form_state['values']['name'])));
  watchdog('taxonomy', 'Deleted vocabulary %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
  $form_state['redirect'] = 'admin/systemreform/taxonomy';
  return;
}

function system_reform_taxonomy_vocabulary_edit($vid) {
  if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) || isset($_POST['confirm'])) {
    $output = drupal_get_form('system_reform_taxonomy_vocabulary_confirm_delete', $vid);
    return $output;
  }
  $output = drupal_get_form('system_reform_taxonomy_vocabulary_form', (array) taxonomy_vocabulary_load($vid));
  return $output;
}

function system_reform_taxonomy_vocabulary_add() {
  $output = drupal_get_form('system_reform_taxonomy_vocabulary_form');
  return $output;
}

function system_reform_taxonomy_vocabulary_form(&$form_state, $edit = array()) {
  $form = taxonomy_form_vocabulary($form_state, $edit);
  //$unsets = array('identification' => array('help', 'description'), 'settings' => array('tags', 'multiple', 'required', 'weight'));
  $unsets = system_reform_get_config('taxonomy');
  system_reform_unset($form,$unsets['vocabulary']);
  system_reform_preprocess_unset($form, array('settings'));
  return $form;
}

function system_reform_taxonomy_vocabulary_form_submit($form, &$form_state) {
  taxonomy_form_vocabulary_submit($form, $form_state);
  $form_state['redirect'] = 'admin/systemreform/taxonomy';
  return;
}

/*
 *  terms
 */

function system_reform_taxonomy_overview_terms($vid) {
  if ($_POST['op'] == t('Reset to alphabetical')) {
    //return taxonomy_vocabulary_confirm_reset_alphabetical($form_state, $vocabulary->vid);
    return drupal_get_form('system_reform_taxonomy_vocabulary_confirm_reset_alphabetical', $vid);
  }
  $output = drupal_get_form('system_reform_taxonomy_terms_form', $vid);
  return $output;
}

function system_reform_taxonomy_vocabulary_confirm_reset_alphabetical(&$form_state, $vid) {
  $vocabulary = taxonomy_vocabulary_load($vid);

  $form['type'] = array('#type' => 'value', '#value' => 'vocabulary');
  $form['vid'] = array('#type' => 'value', '#value' => $vid);
  $form['name'] = array('#type' => 'value', '#value' => $vocabulary->name);
  $form['reset_alphabetical'] = array('#type' => 'value', '#value' => TRUE);
  return confirm_form($form, t('Are you sure you want to reset the vocabulary %title to alphabetical order?', array('%title' => $vocabulary->name)), 'admin/systemreform/taxonomy/' . $vid, t('Resetting a vocabulary will discard all custom ordering and sort items alphabetically.'), t('Reset to alphabetical'), t('Cancel'));
}

function system_reform_taxonomy_vocabulary_confirm_reset_alphabetical_submit($form, &$form_state) {
  db_query('UPDATE {term_data} SET weight = 0 WHERE vid = %d', $form_state['values']['vid']);
  drupal_set_message(t('Reset vocabulary %name to alphabetical order.', array('%name' => $form_state['values']['name'])));
  watchdog('taxonomy', 'Reset vocabulary %name to alphabetical order.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
  $form_state['redirect'] = 'admin/systemreform/taxonomy/' . $form_state['values']['vid'];
}

function system_reform_taxonomy_terms_form(&$form_state, $vid) {
  $form = taxonomy_overview_terms($form_state, taxonomy_vocabulary_load($vid));
  $form['#theme'] = 'taxonomy_overview_terms';
  $form['#submit'][] = 'taxonomy_overview_terms_submit';
  foreach ($form as $key => $value) {
    if (preg_match('/tid:[0-9]:[0-9]/', $key)) {
      $form[$key]['edit']['#value'] = '<a href="' . url('admin/systemreform/taxonomy/edit/term/' . $value['#term']['tid']) . '?destination=admin/systemreform/taxonomy/' . $vid . '">' . t('edit') . '</a>';
    }
  }
  return $form;
}

function system_reform_taxonomy_add_term_page($vid) {
  $vocabulary = taxonomy_vocabulary_load($vid);
  drupal_set_title(t('Add term to %vocabulary', array('%vocabulary' => $vocabulary->name)));
  $output = drupal_get_form('system_reform_taxonomy_term_form', $vocabulary);
  return $output;
}

function system_reform_taxonomy_term_form(&$form_state, $vocabulary, $edit = array()) {
  $form = taxonomy_form_term($form_state, $vocabulary, $edit);
  $form['#submit'][] = 'taxonomy_form_term_submit';
  $unsets = system_reform_get_config('taxonomy');
  //$unsets = array('advanced' => array('relations', 'synonyms', 'weight', 'parent')); //todo,把不需要显示的东西都unset掉，可以考虑扩展为数据库管理这个变量
  system_reform_unset($form, $unsets['term']);
  system_reform_preprocess_unset($form, array('advanced'));
  return $form;
}

function system_reform_taxonomy_term_edit($tid) {
  if ((isset($_POST['op']) && $_POST['op'] == t('Delete')) || isset($_POST['confirm'])) {
    return drupal_get_form('system_reform_taxonomy_term_confirm_delete', $tid);
  }
  if ($term = (array) taxonomy_get_term($tid)) {
    return drupal_get_form('system_reform_taxonomy_term_form', taxonomy_vocabulary_load($term['vid']), $term);
  }
  return drupal_not_found();
}

function system_reform_taxonomy_term_confirm_delete(&$form_state, $tid) {
  $term = taxonomy_get_term($tid);

  $form['type'] = array('#type' => 'value', '#value' => 'term');
  $form['name'] = array('#type' => 'value', '#value' => $term->name);
  $form['tid'] = array('#type' => 'value', '#value' => $tid);
  $form['delete'] = array('#type' => 'value', '#value' => TRUE);
  return confirm_form($form, t('Are you sure you want to delete the term %title?', array('%title' => $term->name)), 'admin/systemreform/taxonomy', t('Deleting a term will delete all its children if there are any. This action cannot be undone.'), t('Delete'), t('Cancel'));
}

function system_reform_taxonomy_term_confirm_delete_submit($form, &$form_state) {
  taxonomy_del_term($form_state['values']['tid']);
  taxonomy_check_vocabulary_hierarchy($form['#vocabulary'], $form_state['values']);
  drupal_set_message(t('Deleted term %name.', array('%name' => $form_state['values']['name'])));
  watchdog('taxonomy', 'Deleted term %name.', array('%name' => $form_state['values']['name']), WATCHDOG_NOTICE);
  $form_state['redirect'] = 'admin/systemreform/taxonomy';
  return;
}

?>